package com.liujie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujie.common.CustomException;
import com.liujie.entity.Category;
import com.liujie.entity.Dish;
import com.liujie.entity.Setmeal;
import com.liujie.mapper.CategoryMapper;
import com.liujie.mapper.SetmealMapper;
import com.liujie.service.CategoryService;
import com.liujie.service.DishService;
import com.liujie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public boolean remove(long id) {
        //查询被删除的分类是否关联菜品，若关联，则无法删除抛出异常
        LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId,id);
        List<Dish> dishes = dishService.list(lqw);
        if (dishes.size()>0){
            throw new CustomException("分类已关联菜品，无法删除");
        }

        //查询被删除的分类是否关联套餐，若关联，则无法删除抛出异常
        LambdaQueryWrapper<Setmeal> lqw1=new LambdaQueryWrapper<>();
        lqw1.eq(Setmeal::getCategoryId,id);
        List<Setmeal> setmeals = setmealService.list(lqw1);
        if (setmeals.size()>0){
            throw new CustomException("分类已关联套餐，无法删除");
        }

        //没有关联，执行删除
        return this.removeById(id);
    }
}
